#    tribalgrowth is a growth monitor for the online game Tribal Wars.
#    Copyright 2009 Nathan Michaels                                            
#                                                                              
#    This file is part of tribalgrowth.                                        
#                                                                              
#    tribalgrowth is free software: you can redistribute it and/or modify      
#    it under the terms of the GNU General Public License as published by      
#    the Free Software Foundation, either version 2 of the License, or         
#    (at your option) any later version.                                       
#                                                                              
#    tribalgrowth is distributed in the hope that it will be useful,           
#    but WITHOUT ANY WARRANTY; without even the implied warranty of            
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             
#    GNU General Public License for more details.                              
#                                                                              
#    You should have received a copy of the GNU General Public License         
#    along with tribalgrowth.  If not, see <http://www.gnu.org/licenses/>. 

from growthdb import RODB

def FindFastest ():
    # Open the database.
    base = RODB ()

    # Find the latest sample. Assume a coherent database. This should hold
    # as long as villages don't get destroyed.
    select = 'SELECT date FROM %s ORDER BY date' % base.villages[0]
    base.cursor.execute (select)

    # Grab the results of the select statement as a list of tuples.
    dates = base.cursor.fetchall ()

    # Pick the only item in the last tuple.
    latest = dates[-1][0]

    # Loop through all the delta tables and look at their last entries.
    fastest = {'id':None, 'dPoints':0}
    for ID in base.ids:
        ID = str (ID)
        select = 'SELECT dPoints from %s WHERE end_time = "%d"' % ('delta'+ID,
                                                                   latest)
        base.cursor.execute (select)
        dPoints = base.cursor.fetchone()[0]
        if dPoints > fastest['dPoints']:
            fastest['id'] = ID
            fastest['dPoints'] = dPoints

    # Now that we've found the fastest grower, grab the village's name
    # and its player.
    select = 'SELECT name, player from %s WHERE date = "%s"' % (
        'village' + str (fastest['id']), latest)
    base.cursor.execute (select)
    row = base.cursor.fetchone ()
    fastest['name'] = row[0]
    fastest['player'] = row[1]

    # ...and its coordinates.
    select = 'SELECT x, y from villages WHERE id = "%s"' % fastest['id']
    base.cursor.execute (select)
    row = base.cursor.fetchone ()
    fastest['x'] = row[0]
    fastest['y'] = row[1]
    return fastest

if __name__=='__main__':
    f = FindFastest ()
    print """The fastest growing village is called "%s" (id:%s). It's
owned by player #%s and is situated at coordinates (%s, %s).
""" % (f['name'], f['id'], f['player'], f['x'], f['y'])
